
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="js/jsAddress.js"></script>
  <title>e网批发管理后台</title>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <script type="text/JavaScript">
    <!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<?php 
include("common/conn.php");  
include("tool/UpFileTool.class.php");

if($_GET['action']=="ok"){
    if ($_FILES[images][name]!='') {
      $UpFileTool=new UpFileTool('images');
      $UpFileTool->upImage($w=500, $h=500 ,$sumbimage='sumbimage', $srcimage='srcimage');
      $images=$UpFileTool->sumbImage;
      $srcimages=$UpFileTool->srcImage;

      //获取最小sort
      $_query = mysql_query("SELECT * FROM homefenlei  ORDER BY  homefenlei.sort  LIMIT 1");
      $_homefenlei = mysql_fetch_assoc($_query);
      //插入新的记录
      $query = mysql_query("INSERT INTO homefenlei (homefenlei.type,name,images,srcimages) VALUES ('".$_POST[type]."','".$_POST[name]."','".$images."','".$srcimages."')");

      $id = mysql_insert_id();
      // 更新排序 
      $query = mysql_query("UPDATE homefenlei set sort=".($_homefenlei[sort]-1)." where id=".$id);
      // 更新排序 end
    
      //在活动图数据库表，新增该首页分类下所有厂商分类和“全部”
      foreach ($_POST[cateids] as $value) {
         $result = mysql_query("select * from h_activity_fenl_tu  where activity_id=".$id."  and cateid=".$value);
         $num_rows = mysql_num_rows($result);
         if($num_rows==0){
              $query = mysql_query("INSERT INTO h_activity_fenl_tu (cateid,activity_id) VALUES ('".$value."','".$id."')");
         }
      }

    echo "<script>alert('添加成功!');window.location.href='homeFenlei_cat_tu.php?id=".$id."'</script>";
  }else{
    echo "<script>alert('未上传图片!');window.location.href='addhomeFenlei.php'</script>";
  }
  
}


if($_GET['ch']=="ok"){
  $query = mysql_query("UPDATE homefenlei set homefenlei.type='".$_POST[type]."',name='".$_POST[name]."' where id=".$_POST[id]);
  if ($_FILES[images][name]!='') {
    $UpFileTool=new UpFileTool('images');
    $UpFileTool->upImage($w=500, $h=500 ,$sumbimage='sumbimage', $srcimage='srcimage');
    $images=$UpFileTool->sumbImage;
    $srcimages=$UpFileTool->srcImage;
    $query = mysql_query("UPDATE homefenlei set images='".$images."',srcimages='".$srcimages."' where id=".$_POST[id]);
  }

  //在活动图数据库表，新增该首页分类下所有厂商分类和“全部”
  //先增加新增分类
  foreach ($_POST[cateids] as $value){
     $result = mysql_query("select * from h_activity_fenl_tu  where activity_id=".$_POST[id]."  and cateid=".$value);
     $num_rows = mysql_num_rows($result);
     if($num_rows==0){
         $query =  mysql_query("INSERT INTO h_activity_fenl_tu (cateid,activity_id) VALUES ('".$value."','".$_POST[id]."')");
     }
  }
  //后删除减少分类
  $query = mysql_query("select * from h_activity_fenl_tu  where activity_id=".$_POST[id]);
  while ($row = mysql_fetch_assoc($query)) {
    if(!in_array($row[cateid],$_POST[cateids]) && $row[cateid]!=0){
       $query =  mysql_query("delete from h_activity_fenl_tu where id=".$row[id]);
    }
  }

  echo "<script>alert('更新成功!');window.location.href='homeFenlei_cat_tu.php?id=".$_POST[id]."'</script>";
}

// 查询首页分类信息
if ($_GET[id]!='') {
  $query = mysql_query("select * from homefenlei where id=".$_GET[id]);
  $homefenlei = mysql_fetch_assoc($query);
  //获取该首页分类的厂商分类
  $query = mysql_query("select cateid from h_activity_fenl_tu  where activity_id=".$_GET[id]);
  while ($row = mysql_fetch_assoc($query)) {
     $fenlei[] = $row[cateid];
  }
  $fenlei = implode(',', $fenlei);
}
// 查询首页分类信息 end

// 查询categroy表信息
$query = mysql_query("select * from categroy");
while ($row = mysql_fetch_assoc($query)) {
  $categroy[] = $row;
}
// 查询categroy表信息 end
?>

<body>

  <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->
  <div id="sub_nav">

    <div id="subnav_wrap" class="container_12">
       <!-- start sub nav list -->
      <div id="subnav" class=" grid_12">
        <ul>
          <li><a href="homeFenlei.php">返回首页分类列表</a></li>
          <li><a href="addhomeFenlei.php?id=<?php echo $_REQUEST[id];?>"  class="sub_nav_active">编辑首页分类</a></li>
          <li><a href="homeFenlei_cat_tu.php?id=<?php echo $_REQUEST[id];?>">厂商分类图片</a></li>
        </ul>
      </div>

    </div>


  </div>

  <div class="container_12">
    <form action="?<?php if($_GET['id']!=""){echo "ch=ok";}else{echo "action=ok";} ?>" method="post" enctype="multipart/form-data"> 
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="30%" scope="col"><label>首页分类名称</label></td>
          <td width="50%" scope="col">
            <input name="name" type="text" class="text"  value="<?php echo $homefenlei[name];?>"/>
          </td>
        </tr>
        <tr>
          <td width="30%" scope="col"><label>首页分类图片</label></td>
          <td width="50%" scope="col">
            <input name="images" type="file" class="text"/>
          </td>
        </tr>
        <?php if ($homefenlei[images]!='') { ?>
        <tr>
          <td width="30%" scope="col"></td>
          <td width="50%" scope="col">
            <img src="<?php echo $homefenlei[images]; ?>" style="height:150px;width:150px;">
          </td>
        </tr>
        <?php } ?>
        <tr>
          <td width="30%" scope="col"><label>打开类型</label></td>
          <td width="50%" scope="col">
            <select  name="type">
                  <option <?php echo  $homefenlei[type]==1?'selected="selected"':'';?>  value="1">源头厂家</option>
                  <option <?php echo  $homefenlei[type]==2?'selected="selected"':'';?> value="2">本地市场</option>
                  <option <?php echo  $homefenlei[type]==3?'selected="selected"':'';?> value="3">打开厂商</option>
                  <option <?php echo  $homefenlei[type]==4?'selected="selected"':'';?> value="4">打开商品</option>
                  <option <?php echo  $homefenlei[type]==5?'selected="selected"':'';?> value="5">更多</option>
            </select>
          </td>
        </tr>
        <tr>
          <td width="30%" scope="col"><label>包含的分类</label></td>
          <td width="50%" scope="col" style="text-align: left;">
            <span  style="display:none">
               <input type="checkbox"  checked='checked'  name="cateids[]" value="0">
            </span>
            <?php foreach ($categroy as $k =>$v) { ?>
                <input type="checkbox" <?php if (strpos($fenlei, $v[cateid]) || strpos($fenlei, $v[cateid]) === 0) { ?> checked='checked' <?php } ?> name="cateids[]" value="<?php echo $v[cateid]; ?>">
                <?php echo $v[name]; ?>
                <?php
                if (($k+1)%4 == 0) {
                  echo "<br />";
                }
                ?>
            <?php } ?>
          </td>
        </tr>
        <tr>
          <td width="40%" scope="col"></td>
          <td width="50%" scope="col"><input name="id" type="hidden" value="<?php echo $_GET[id];?>" />
            <input type="submit" value="提交" class="submit" />
          </td>
        </tr>
        </table>

      </form>
    </div>

  </form>


</body>
</html>
